﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace HeavenvsDarkness
{

    public class VisibleGameEntity : GameEntity
    {
        public const int LEFT = 4;
        public const int RIGHT = 6;
        public const int UP = 8;
        public const int DOWN = 2;
        protected Matrix rotation;

        public Matrix Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        protected Boolean isStop;

        public Boolean IsStop
        {
            get { return isStop; }
            set { isStop = value; }
        }

        protected float scale;

        public float Scale
        {
            get { return scale; }
            set { scale = value; }
        }
        protected Vector2 size;

        public Vector2 Size
        {
            get { return size; }
            set { size = value; }
        }
        protected Vector3 position;

        public Vector3 Position
        {
            get { return position; }
            set 
            { 
                position = value;
            }
        }

        Vector3 destPost;

        public Vector3 DestPost
        {
            get { return destPost; }
            set 
            { 
                destPost = value;
                for (int i = 0; i < Items.Count; i++)
                {
                    Items[i].DestPost = DestPost;
                }
            }
        }

        List<VisibleGameEntity> items;

        public List<VisibleGameEntity> Items
        {
            get { return items; }
            set { items = value; }
        }

        int nItem;

        public int NItem
        {
          get { return nItem; }
          set { nItem = value; }
        }

        Boolean _isFlipHorizonetal = false;

        public Boolean isFlipHorizonetal
        {
            get { return _isFlipHorizonetal; }
            set { _isFlipHorizonetal = value; }
        }

        float speedMove;

        public float SpeedMove
        {
            get { return speedMove; }
            set { speedMove = value; }
        }
        String name;

        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        protected MyPlanarModel model;

        public MyPlanarModel Model
        {
            get { return model; }
            set { model = value; }
        }

        public VisibleGameEntity()
        {
            Items = new List<VisibleGameEntity>();
        }


        override public void Update(GameTime gameTime)
        {          
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].Update(gameTime);
            }
        }



        override public void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].Draw(spriteBatch);
            }
        }

        virtual public Boolean ProcessKeyInput()
        {
            return false;
        }
       

        virtual public int GetIndex()
        {
            return -1;
        }
       
        public override void UpdatePosition(Vector3 newPos)
        {
            Vector3 temp = Position;
            temp.X += newPos.X;
            temp.Y += newPos.Y;
            Position = temp;
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].UpdatePosition(newPos);
            }
        }
        public virtual void UpdatePosition()
        {
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].Position =  Position;
                Items[i].UpdatePosition();
            }
        }
      

     

        public virtual void UpdateSize()
        {
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].Size = Size;
            }
        }

        public virtual bool ProcessMouseInput()
        {
            return false;
        }

        public override void ProcessUserInput()
        {

        }

        public virtual bool CanBeSwitch()
        {
            return false;
        }

        public virtual void SetIndex(int id)
        {

        }
        public virtual void SetAllFlipHorizonetal(Boolean isFlip)
        {
            isFlipHorizonetal = isFlip;
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].isFlipHorizonetal = isFlipHorizonetal;
            }
        }

        

        public virtual void MoveTo(Vector3 Dest,float Delta)
        {
            if (Position != DestPost)
            {            
                if (Vector3.Distance(Dest, Position) < Delta)
                {
                    Position = Dest;
                }
                else
                {
                    Position += Vector3.Normalize(Dest - Position) * Delta;
                }
            }
            UpdatePosition();
            

        }

        public VisibleGameEntity(Vector3 newPos)
        {
            Position = newPos;
        }


        public virtual Vector2 GetTextureSize()
        {
            for (int i = 0; i < Items.Count; i++)
            {
                Vector2 temp = Items[i].GetTextureSize();
                if (temp != Vector2.Zero)
                {
                    return temp;
                }
            }
            return Vector2.Zero;
        }

        public virtual Color[] GetTextureData()
        {
            return null;
        }

        public virtual Color GetColorAtPos(float x, float y)
        {
            return new Color(0, 0, 0, 0);
        }

        public virtual Texture2D GetTexture()
        {
            return null;
        }

        public virtual void SetScale(float scale)
        {
            Scale = scale;
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].SetScale(scale);
            }
        }

        public virtual void SetPosition(Vector3 newPos)
        {
            Position = newPos;
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].SetPosition(newPos);
            }
        }

        public virtual Boolean isCursorStayed()
        {
            Ray cursorRay = Global.GetCursor().CalculateCursorRay(GameUtil.Camera.Projection, GameUtil.Camera.View);
            return false;
        }

        public virtual void UpdateProperties()
        {
            for (int i = 0; i < Items.Count; i++)
            {
                Items[i].Position = Position;
                Items[i].Rotation = Rotation;
                Items[i].Scale = Scale;
            }
        }
    }
}
